package me.raih.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author Raih
 * @Time 2020/12/14 16:46
 *
 * 用户拦截器
 *
 */
public class UserHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object login_flag = request.getSession().getAttribute("USER_LOGIN_SESSION_FLAG");
        if(Objects.isNull(login_flag)){
            //没有登录标志
            //request.setAttribute("msg","此操作需要登录");
            //request.getRequestDispatcher("user/login.html").forward(request,response);
            //response.sendRedirect("user/login");
            response.sendRedirect("/user/login");
            return false;
        }else {
            return true;
        }
    }
}
